package me.keiwu.pub.data;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

/**
 * Created by kei on 23/05/2017.
 */
public class Response {

    private Logger logger = LoggerFactory.getLogger(Response.class);

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private Object msg;

    private Response(int code, Object msg) {
        logger.info("Response create response: code={}, msg={}", code, msg);
        this.code = code;
        this.msg = msg;
    }


    public static Response ok() {
        return getResponse(HttpStatus.OK.value());
    }

    public static Response ok(Object data) {
        return getResponse(HttpStatus.OK.value(), data);
    }

    public static Response ok(int code, Object data) {
        return getResponse(code, data);
    }

    public static Response fail() {
        return getResponse(HttpStatus.BAD_REQUEST.value());
    }

    public static Response fail(Object data) {
        return getResponse(HttpStatus.BAD_REQUEST.value(), data);
    }

    public static Response fail(int code, Object data) {
        return getResponse(code, data);
    }


    private static Response getResponse(int code) {
        return new Response(code, "");
    }

    private static Response getResponse(int code, Object msg) {
        return new Response(code, msg);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
